package com.huoyun.common.action;

import com.huoyun.announcement.bean.Notice;
import com.huoyun.announcement.bean.NoticeFactory;
import com.huoyun.announcement.service.NoticeService;
import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Result;
import com.huoyun.car.bean.Car;
import com.huoyun.car.service.CarService;
import com.huoyun.common.bean.InviteCar;
import com.huoyun.common.service.InviteCarService;

public class InviteAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private long inviteId;

	private long goodsId;

	private long carId;

	private InviteCar inviteCar;

	private InviteCarService inviteCarService;

	private Result result;

	private NoticeService noticeService;
	private NoticeFactory noticeFactory;

	private CarService carService;

	public String preInviteCar() {
		inviteCar = inviteCarService.getInviteInfo(goodsId, carId);

		// 用户已经邀请过车辆了
		if (inviteCar != null) {
			this.setResultMsg("您已经邀请过了，请不要重复邀请！");
			return ERROR;
		}
		return SUCCESS;
	}

	public String inviteCar() {
		result = new Result();

		InviteCar oldInvite = inviteCarService.getInviteInfo(goodsId, carId);

		// 用户已经邀请过车辆了
		if (oldInvite != null) {
			result.setResult(Result.ERROR);
			result.setMessage("您已经邀请过了，请不要重复邀请！");

			return ERROR;
		}

		try {
			// 状态为0表示等待车主应标
			inviteCar.setStatus(0);

			inviteCarService.saveInviteCar(inviteCar);
			result.setResult(Result.SUCCESS);
			result.setMessage("邀请成功");
			// 发出应标邀请通知
			Car car = carService.getCarById(inviteCar.getCarId());
			Notice notice = NoticeFactory.newBiddingInvitation(car.getUserId(), goodsId);
			noticeService.postNotice(notice);

		} catch (Exception e) {
			result.setResult(Result.ERROR);
			result.setMessage("邀请失败");

			return ERROR;
		}
		return SUCCESS;
	}

	public long getInviteId() {
		return inviteId;
	}

	public void setInviteId(long inviteId) {
		this.inviteId = inviteId;
	}

	public InviteCar getInviteCar() {
		return inviteCar;
	}

	public void setInviteCar(InviteCar inviteCar) {
		this.inviteCar = inviteCar;
	}

	public InviteCarService getInviteCarService() {
		return inviteCarService;
	}

	public void setInviteCarService(InviteCarService inviteCarService) {
		this.inviteCarService = inviteCarService;
	}

	public long getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}

	public long getCarId() {
		return carId;
	}

	public void setCarId(long carId) {
		this.carId = carId;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public NoticeFactory getNoticeFactory() {
		return noticeFactory;
	}

	public void setNoticeFactory(NoticeFactory noticeFactory) {
		this.noticeFactory = noticeFactory;
	}

}
